package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.common.PageResult;
import com.example.dto.PurchaseRecordQueryDTO;
import com.example.service.AdminPurchaseService;
import com.example.vo.PurchaseRecordVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员购买记录管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/purchase")
@CrossOrigin
@RequiredArgsConstructor
public class AdminPurchaseController {

    private final AdminPurchaseService adminPurchaseService;

    /**
     * 分页查询所有用户的购买记录
     */
    @GetMapping("/records/page")
    @RequirePermission("admin:purchase:records:view")
    public Result<PageResult<PurchaseRecordVO>> getPurchaseRecordsPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String storeManagerName,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        PurchaseRecordQueryDTO queryDTO = new PurchaseRecordQueryDTO();
        queryDTO.setPage(current);
        queryDTO.setSize(size);
        queryDTO.setUserName(userName);
        queryDTO.setProductName(productName);
        queryDTO.setProductCode(productCode);
        queryDTO.setStoreManagerName(storeManagerName);
        queryDTO.setStatus(status);
        queryDTO.setStartTime(startTime);
        queryDTO.setEndTime(endTime);
        
        return adminPurchaseService.getPurchaseRecordsPage(queryDTO);
    }

    /**
     * 根据用户ID查询购买记录
     */
    @GetMapping("/records/user/{userId}")
    @RequirePermission("admin:purchase:records:view")
    public Result<List<PurchaseRecordVO>> getPurchaseRecordsByUserId(@PathVariable Long userId) {
        return adminPurchaseService.getPurchaseRecordsByUserId(userId);
    }

    /**
     * 根据商品ID查询购买记录
     */
    @GetMapping("/records/product/{productId}")
    @RequirePermission("admin:purchase:records:view")
    public Result<List<PurchaseRecordVO>> getPurchaseRecordsByProductId(@PathVariable Long productId) {
        return adminPurchaseService.getPurchaseRecordsByProductId(productId);
    }

    /**
     * 根据店长ID查询购买记录
     */
    @GetMapping("/records/store-manager/{storeManagerId}")
    @RequirePermission("admin:purchase:records:view")
    public Result<List<PurchaseRecordVO>> getPurchaseRecordsByStoreManagerId(@PathVariable Long storeManagerId) {
        return adminPurchaseService.getPurchaseRecordsByStoreManagerId(storeManagerId);
    }

    /**
     * 查询购买记录详情
     */
    @GetMapping("/records/{id}")
    @RequirePermission("admin:purchase:records:view")
    public Result<PurchaseRecordVO> getPurchaseRecordById(@PathVariable Long id) {
        return adminPurchaseService.getPurchaseRecordById(id);
    }

    /**
     * 导出购买记录
     */
    @GetMapping("/records/export")
    @RequirePermission("admin:purchase:records:export")
    public Result<List<PurchaseRecordVO>> exportPurchaseRecords(
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        PurchaseRecordQueryDTO queryDTO = new PurchaseRecordQueryDTO();
        queryDTO.setUserName(userName);
        queryDTO.setProductName(productName);
        queryDTO.setStatus(status);
        queryDTO.setStartTime(startTime);
        queryDTO.setEndTime(endTime);
        
        return adminPurchaseService.exportPurchaseRecords(queryDTO);
    }

    /**
     * 获取购买统计信息
     */
    @GetMapping("/statistics")
    @RequirePermission("admin:purchase:records:view")
    public Result<Object> getPurchaseStatistics(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        return adminPurchaseService.getPurchaseStatistics(startTime, endTime);
    }

    /**
     * 管理员取消用户购买记录
     */
    @PutMapping("/records/{id}/cancel")
    @RequirePermission("admin:purchase:records:cancel")
    public Result<Void> cancelPurchaseRecord(@PathVariable Long id, 
                                             @RequestParam(required = false) String reason) {
        return adminPurchaseService.cancelPurchaseRecord(id, reason);
    }

    /**
     * 获取热门商品购买排行
     */
    @GetMapping("/popular-products")
    @RequirePermission("admin:purchase:records:view")
    public Result<List<Object>> getPopularProducts(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        return adminPurchaseService.getPopularProducts(limit, startTime, endTime);
    }

    /**
     * 获取用户购买排行
     */
    @GetMapping("/top-buyers")
    @RequirePermission("admin:purchase:records:view")
    public Result<List<Object>> getTopBuyers(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        return adminPurchaseService.getTopBuyers(limit, startTime, endTime);
    }
} 